import localSave from './localSave'
import axios from 'axios'
import { AxiosRequestConfig, AxiosResponse } from 'axios'
//import { message } from 'ant-design-vue'
//import { useRouter } from 'vue-router'
import router from '@/router'
//const router = useRouter()
axios.interceptors.response.use((response: any) => {
  if (Number(response.status) === 401 || Number(response.data.status) === 401) {
    router.push('/login')
  }
  return response
})
class Request {
  get<T>(url: string, params?: object): Promise<T> {
    const config: AxiosRequestConfig = {
      url: '/api' + url,
      method: 'GET',
      params,
      headers: {
        'Content-Type': 'application/json;charset=UTF-8',
        role: 1,
        mt: localSave.getSession('mt') ? localSave.getSession('mt') : undefined
      }
    }
    return axios(config).then((res: AxiosResponse) => {
      if (res.status === 200) {
        return res.data
      }
    })
  }
  post<T>(url: string, data: object): Promise<T> {
    const config: AxiosRequestConfig = {
      url: '/api' + url,
      method: 'POST',
      data,
      headers: {
        'Content-Type': 'application/json;charset=UTF-8',
        role: 1,
        mt: localSave.getSession('mt') ? localSave.getSession('mt') : undefined
      }
    }
    return axios(config).then((res: AxiosResponse) => {
      if (res.status === 200) {
        return res.data
      }
    })
  }
  put<T>(url: string, data: object): Promise<T> {
    const config: AxiosRequestConfig = {
      url: '/api' + url,
      method: 'PUT',
      data,
      headers: {
        'Content-Type': 'application/json;charset=UTF-8',
        role: 1,
        mt: localSave.getSession('mt') ? localSave.getSession('mt') : undefined
      }
    }
    return axios(config).then((res: AxiosResponse) => {
      if (res.status === 200) {
        return res.data
      }
    })
  }
}
export default new Request()
